package com.truckdispatching.http.controller;

import java.util.HashMap;

import org.json.JSONObject;

import com.truckdispatching.UserManager;
import com.truckdispatching.http.util.TDHttpController;

public class LoginHttpController extends TDHttpController {

	private String username;
	private String password;

	@Override
	public String getRequestUrl() {
		return mHostUrl + "/wap/login.do";
	}

	public void sendRequest(String username, String password) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("user.USRNAME", username);
		params.put("user.USRPWD", password);
		this.username = username;
		this.password = password;
		sendRequest(params,true);
	}

	@Override
	public void onSuccess(String response) {
		try {
			JSONObject jo = getJSONObjectFromJSON(new JSONObject(response), "data", null);
			String token = getStringFromJSON(jo, "token", "");
			String agent = getStringFromJSON(jo, "agent", "");
			if(agent.equals("null")){
				agent = "";
			}
			boolean truckStatus = getBooleanFromJSON(jo, "truckStatus", false);
			UserManager.setIsLogin(true);
			UserManager.setToken(token);
			UserManager.setAgent(agent);
			UserManager.setTruckStatus(truckStatus);
			UserManager.setMobile(username);
			UserManager.setPassword(password);
			callOnResponseListener(null);
		} catch (Exception e) {
			callOnResponseErrorListener(-1, "JSON数据异常");
		}
	}

	@Override
	public void onError(int errorCode, String errorMessage) {
		callOnResponseErrorListener(errorCode, errorMessage);
	}

}
